package com.init.iblog.Controller;


import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.TagListDto;
import com.init.iblog.domain.entity.LoginUser;
import com.init.iblog.domain.entity.Tag;
import com.init.iblog.domain.entity.User;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.domain.vo.TagVo;
import com.init.iblog.service.TagService;
import com.init.iblog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 查询标签
     * @param pageNum
     * @param pageSize
     * @param tagListDto
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, TagListDto tagListDto){
        System.out.println("==============/list接口分页查询标签=============");
        return tagService.pageTagList(pageNum,pageSize,tagListDto);
    }

    /**
     * 添加标签
     * @param tag
     * @return
     */
    @PostMapping
    public ResponseResult addTag(@RequestBody Tag tag){
        System.out.println("=================/content/tag接口：添加标签==============");
        //获取当前登录用户
        User user = SecurityUtils.getLoginUser().getUser();
        Long create_by = user.getId();
        tag.setCreateBy(create_by);
        tag.setCreateTime(new Date());
        return tagService.addTag(tag);
    }

    /**
     * 逻辑删除标签
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteTag(@PathVariable List<Long> id){

        System.out.println("===========进入删除标签接口===========");

        return tagService.deleteTag(id);
    }

    /**
     * 获取标签信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getTag(@PathVariable Long id){

        System.out.println("=========进入查询标签信息接口=============");

        return tagService.getTag(id);
    }

    /**
     * 修改标签信息
     * @param tag
     * @return
     */
    @PutMapping
    public ResponseResult updateTag(@RequestBody Tag tag){

        System.out.println("===============进行修改标签接口=============");
        return tagService.updateTag(tag);
    }


    /**
     * 查询所有标签接口
     * @return
     */
    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        System.out.println("==========进入查询所有标签接口==============");
        List<TagVo> list = tagService.listAllTag();
        return ResponseResult.okResult(list);
    }

}
